package view
{
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import instance.ActivePolicy;
	
	import util.GraphicsResource;
	
	/**
	 *
	 *	动画对象，比如英雄的某个动作 
	 */ 
	public class AnimatedObject extends GameObject
	{
		protected var currentFrame:int = 0;
		protected var frameTime:Number = 0;
		protected var playOnce:Boolean = false;
		
		public function AnimatedObject(activePolicy:Function)
		{
			super(activePolicy);
		}
		
		public function startUpAnimatedObject(graphics:GraphicsResource,position:Point,layer:String,playOnce:Boolean=false):void {
			super.startUpGameObject(graphics,position,layer);
		}
		
		override public function enterFrame(dt:Number):void {
			if(inuse)
			{
				frameTime += dt;
				if (graphics.fps != -1)
				{
					while (frameTime > 1/graphics.fps)
					{
						frameTime -= 1/graphics.fps;
						currentFrame = (currentFrame + 1) % graphics.frames;
						if (currentFrame == 0 && playOnce)
						{
//							shutDown();
							changeState();
							break;
						}
					}
				}
				super.enterFrame(dt);
			}
		}
		
		protected function changeState():void {
			
		}
		
		override public function copyToBackBuffer(db:BitmapData):void {
			var rect:Rectangle = new Rectangle(currentFrame * graphics.frameWidth,0,graphics.frameWidth,graphics.frameHeight);
			db.copyPixels(graphics.bitmap,rect,position,graphics.bitmapAlpha, new Point(rect.x, 0), true);
		}
		
		public function changeFrame(graphics:GraphicsResource):void {
			this.graphics = graphics;
			this.currentFrame = 0;
			this.frameTime = 0;
		}
	}
}